//
//  HMHomeTableViewController.swift
//  Weibo
//
//  Created by Apple on 2017/7/10.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
import YYModel

class HMHomeTableViewController: HMVisitorTableViewController {

    //  引用ViewModel
    fileprivate lazy var homeViewModel: HMHomeViewModel = HMHomeViewModel()
    //  MARK:   --懒加载控件
    fileprivate lazy var pullUpView: UIActivityIndicatorView = {
        let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        indicatorView.color = UIColor.red
        return indicatorView
    }()
//    fileprivate lazy var pullDownView: UIRefreshControl = {
//        let pullDownView = UIRefreshControl()
//        pullDownView.addTarget(self, action: #selector(pullDownAction), for: .valueChanged)
//        return pullDownView
//    }()
    
    fileprivate lazy var pullDownView: HMRefreshControl = {
        let pullDownView = HMRefreshControl()
        pullDownView.addTarget(self, action: #selector(pullDownAction), for: .valueChanged)
        return pullDownView
    }()
    
    fileprivate lazy var tipLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.white)
        label.backgroundColor = UIColor.orange
        label.text = "没有加载到最新的微博"
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !isLogin {
            visitorView?.updateVisitorInfo(message: nil, imageName: nil)
        } else {
            setupUI()
            loadData()
        }
    }
    
    //  设置tableView相关操作
    private func setupTableView() {
        //  注册cell
        tableView.register(HMHomeTableViewCell.self, forCellReuseIdentifier: HomeCellIdentifier)
        //  自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        //  预估高度
        tableView.estimatedRowHeight = 200
        //  去掉分割线
        tableView.separatorStyle = .none
        //  设置底部视图
        tableView.tableFooterView = pullUpView
        pullUpView.sizeToFit()
    
        tableView.addSubview(pullDownView)
        
    }
    
    private func setupUI() {
        if let nav = self.navigationController {
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
            tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35, width: nav.navigationBar.width, height: 35)
        }
        setupTableView()
    }
    
    //  加载微博数据
    fileprivate func loadData() {
        homeViewModel.loadData(isUp: pullUpView.isAnimating) { (isSuccess, message) in
            if self.pullUpView.isAnimating == false {
                //  不是上拉加载执行动画
                self.startTipAnimation(message: message)
            }
            
            self.endRefreshing()
            if isSuccess {
                self.tableView.reloadData()
            }
        }
    }
    
    //  开启tip动画
    private func startTipAnimation(message: String) {
        //  如果label真正显示说明正在执行动画，那么不然其再次执行动画
        if tipLabel.isHidden == false {
            return
        }
        //  显示label
        tipLabel.isHidden = false
        tipLabel.text = message
        
        UIView.animate(withDuration: 1, animations: { 
            self.tipLabel.transform = CGAffineTransform(translationX: 0, y: self.tipLabel.height)
        }) { (_) in
            UIView.animate(withDuration: 1, animations: { 
                self.tipLabel.transform = CGAffineTransform.identity
            }, completion: { (_) in
                self.tipLabel.isHidden = true
            })
        }
        
    }
    
    
    //  MARK:   --下拉刷新
    @objc private func pullDownAction() {
        print("下拉刷新")
        loadData()
    }
    
    //  结束刷新
    private func endRefreshing() {
        pullUpView.stopAnimating()
        pullDownView.endRefreshing()
    }
    
}

//  MARK:   --UITableViewDataSource
extension HMHomeTableViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeViewModel.statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: HomeCellIdentifier, for: indexPath) as! HMHomeTableViewCell
        cell.statusViewModel = homeViewModel.statusList[indexPath.row]
        cell.selectionStyle = .none
        return cell
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.item == homeViewModel.statusList.count - 1 && !pullUpView.isAnimating {
            //  开启风火轮
            pullUpView.startAnimating()
            loadData()
        }
    }
    
}






